#include "vxdockcontainer.h"
#include "vxmousetransmitter.h"
#include "vxeffects.h"
#include "vx.h"

VxDockCouple::VxDockCouple(QPushButton * button, QDockWidget * widget) :
	_button(button),  _widget(widget), _area(Qt::LeftDockWidgetArea)
{
	connect(button, SIGNAL(clicked()), this, SLOT(_placeWidget()));
}


void VxDockCouple::_placeWidget()
{
	QMainWindow * mw = static_cast<QMainWindow *>(_widget->parentWidget());
	_widget->setFloating(false);
	mw->addDockWidget(_area, _widget);
	_widget->showNormal();

	_button->hide();
	emit reorderButtons();

}

VxDockContainer::VxDockContainer(QWidget * parent) :
		QWidget(parent), _hidden(true), _showAnimated(false), _currentArea(Qt::LeftDockWidgetArea)
{
	setVisible(false);
	connect(VxMouseTransmitter::instance(), SIGNAL(mouseReleasedMove(QPoint)), this, SLOT(_mouseReleasedMove(QPoint)));
}


void VxDockContainer::addCouple(VxDockCouple * couple)
{
	_couples.append(couple);
	connect(couple, SIGNAL(reorderButtons()), this, SLOT(hideAll()));
}



QPropertyAnimation * _addButtonAnimation(QWidget * button, int timeix, QRect stgeom, QRect endgeom)
{
	QPropertyAnimation * animation = new QPropertyAnimation(button, "geometry");
	animation->setDuration(580 + timeix*20);
	animation->setEasingCurve(QEasingCurve::InOutBack);
	animation->setStartValue(stgeom);
	animation->setEndValue(endgeom);
	button->setGeometry(stgeom);
	animation->start(QAbstractAnimation::DeleteWhenStopped);
	return animation;
}


void VxDockContainer::showByOrientation(Qt::DockWidgetArea area, bool animFromCurrent)
{
	_hidden = false;

	if (_showAnimated){
		return;
	}

	int w = 0;
	int h = 0;
	int timeix = 1;
	QPropertyAnimation * anim;

	_currentArea = area;

	foreach(VxDockCouple * couple, _couples){

		QDockWidget * dw = couple->widget();
		QWidget * bt = couple->button();

		if (!dw->isAreaAllowed(area) ||	dw->isVisible()){
			bt->hide();
			continue;
		}

		couple->setArea(area);

		QRect newgeom = bt->geometry();

		switch(area){

		case Qt::TopDockWidgetArea:
		case Qt::BottomDockWidgetArea: {
				if (bt->height() > h){
					h = bt->height();
				}
				newgeom.moveTo(w, 0);
				w += bt->width() + 2;
				break;
			}
		case Qt::LeftDockWidgetArea:
		case Qt::RightDockWidgetArea: {
				if (bt->width() > w){
					w = bt->width();
				}
				newgeom.moveTo(0, h);
				h += bt->height() + 2;
				break;
			}
		default:;
		}

		QRect sr;
		if (animFromCurrent){
			sr = bt->geometry();
		} else {
			sr = QRect(bt->width()/2, bt->height()/2, 1,1);
		}

		anim = _addButtonAnimation(bt, timeix++, sr, newgeom);


		bt->show();

		if(anim != NULL){
			_showAnimated = true;
			connect(anim, SIGNAL(finished()), this, SLOT(_animationShowFinished()));
		}

	}
	if (!animFromCurrent)
		setFixedSize(w, h);
}


void VxDockContainer::_animationShowFinished()
{
	_showAnimated = false;
}


void VxDockContainer::hideAll()
{
	VxEffects::hide(this);
}



void VxDockContainer::_mouseReleasedMove(QPoint globalp)
{
	if (_hidden){
		return;
	}

	if (_showAnimated){
		return;
	}

	QPoint p = mapFromGlobal(globalp);
	if (p.x() > 0 && p.x() < width() &&
		p.y() > 0 && p.y() < height()){
		return;
	}

	int dx = 0;
	int dy = 0;

	if (p.x() < 0){
		dx = abs(p.x());
	} else {
		dx = p.x() - width();
	}

	if (p.y() < 0){
		dy = abs(p.y());
	} else {
		dy = p.y() - height();
	}

	if (dx < Vx::DeltaHide && dy < Vx::DeltaHide){
		return;
	}

	_hidden = true;

	hideAll();
}

